package com.yuyang.wifi.views;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.yuyang.wifi.MyApplication;
import com.yuyang.wifi.utils.DeviceInfoUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;

/**
 * created by mej
 * on 2020/12/2
 */
public class LoadCsjInsertAdUtils {

    private Context mContext;
    private boolean mIsLoaded3 = false; //视频是否加载完成
    private TTNativeExpressAd mTTAd3;
    private TTAdNative mTTAd3Native3;
    private TTAdManager ttAdManager3;
    private OnItemAllClickListener onItemAllClickListener;

    static LoadCsjInsertAdUtils instance = new LoadCsjInsertAdUtils();
    private String mInsertAdId;

    public static LoadCsjInsertAdUtils getInstance() {
        return instance;
    }

    //一、加载穿山甲激励视频
    //BDAdvanceBaseAppNative 绑定激励视频回调
    public void load(final Context context, String moduleName) {
        mContext = context;
//        mInsertAdId = insertAdId;
//        String channelName = DeviceInfoUtils.getUmChannelName(mContext);
//        if (!TextUtils.isEmpty(channelName) && "sykq000001".equals(channelName)) {
//            mInsertAdId = "946140331";
//        } else if (!TextUtils.isEmpty(channelName) && "sykq000002".equals(channelName)) {
//            mInsertAdId = "946140331";
//        }
        mInsertAdId = MyApplication.getCsjAdIdChannel(moduleName);
        initAdAppId3();
        loadAd3();
    }

    private void initAdAppId3() {

        if (ttAdManager3 == null) {
            ttAdManager3 = TTAdSdk.getAdManager();
            ttAdManager3.requestPermissionIfNecessary(mContext);
        }
        if (mTTAd3Native3 == null) {
            mTTAd3Native3 = ttAdManager3.createAdNative(mContext);
        }
    }

    public void showInterActionAdVideo() {
        if (mTTAd3 != null && mIsLoaded3) {
            if (null != mContext) {
                mTTAd3.showInteractionExpressAd((Activity) mContext);
            }
        } else {
            loadAd3();
        }
    }

    private void loadAd3() {//"945474388"
        loadInterActionAd3(mInsertAdId, 2, 300, 450);
    }

    private void loadInterActionAd3(String codeId, int orientation, int expressViewWidth, int expressViewHeight) {

        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setOrientation(orientation)
                .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) //期望模板广告view的size,单位dp
                .setImageAcceptedSize(640, 320)//这个参数设置即可，不影响个性化模板广告的size
                .build();
        //加载广告
        mTTAd3Native3.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
//                ToastUtils.showShortToast((Activity) mContext, "load error : " + code + ", " + message);
                Log.e("插屏广告加载失败", code + message + mInsertAdId);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                mIsLoaded3 = false;
                mTTAd3 = ads.get(0);
                bindAdListener(mTTAd3);
                mTTAd3.render();//调用render开始渲染广告
            }

        });
    }

    //绑定广告行为
    private void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
            @Override
            public void onAdDismiss() {
                //ToastUtils.showShortToast((Activity) mContext, "广告关闭");
                if (null != onItemAllClickListener) {
                    onItemAllClickListener.onItemAllClickListener();
                }
                //loadAd3();
            }

            @Override
            public void onAdClicked(View view, int type) {
                //ToastUtils.showShortToast((Activity) mContext, "广告被点击");
            }

            @Override
            public void onAdShow(View view, int type) {
                //ToastUtils.showShortToast((Activity) mContext, "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                //Log.e("ExpressView", "render fail:" + (System.currentTimeMillis() - startTime));
                //ToastUtils.showShortToast((Activity) mContext, msg + " code:" + code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                //ToastUtils.showShortToast((Activity) mContext, "渲染成功");
                mIsLoaded3 = true;
                //在渲染成功回调时展示广告，提升体验
//                mExpressContainer.removeAllViews();
//                mExpressContainer.addView(view);
                // mTTAd3.showInteractionExpressAd((Activity) mContext);
            }


        });

        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
            return;
        }
        //可选，下载监听设置
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                // ToastUtils.showShortToast((Activity) mContext, "点击开始下载");
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {

            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                // ToastUtils.showShortToast((Activity) mContext, "下载暂停，点击继续");
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                //ToastUtils.showShortToast((Activity) mContext, "下载失败，点击重新下载");
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                //ToastUtils.showShortToast((Activity) mContext, "安装完成，点击图片打开");
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                //ToastUtils.showShortToast((Activity) mContext, "点击安装");
            }
        });
    }

    public interface OnItemAllClickListener {
        void onItemAllClickListener();
    }

    public void setItemAllClickListener(OnItemAllClickListener onItemAllClickListener) {
        this.onItemAllClickListener = onItemAllClickListener;
    }

}
